let config = require("./config");

let {hexMD5} = require('../utils/md5');//md5

//保存体脂称客户信息
function AddFat(data,callback){
    wx.request({
        url: config.minapp_url + '/AddFat',
        data: {
          nickname:data.nickname,
          tel: data.tel,
          sex: data.sex,
          birthdate:data.birthdate,
          height:data.height,
          weight:data.weight,
          photo:data.photo,
          openId:data.openId
        },
        method:'POST',
        success: function(e){
                callback&&callback(e);//存在callback才传递数据 否则不传
            }
        })
}

// 修改体脂称客户信息
function UpdateFat(data,callback){
    wx.request({
        url: config.minapp_url + '/UpdateFat',
        data: {
          id:data.id,
          nickname:data.nickname,
          tel: data.tel,
          sex: data.sex,
          birthdate:data.birthdate,
          height:data.height,
          weight:data.weight,
          photo:data.photo,
        },
        method:'POST',
        success: function(e){
                callback&&callback(e);//存在callback才传递数据 否则不传
            }
        })
}

// // 获取体脂称客户信息
// function GetFatModel(tel,callback){
//     wx.request({
//         url: config.minapp_url + '/GetFatModel',
//         data: {
//           tel: tel,
//         },
//         method:'GET',
//         success: function(e){
//                 callback&&callback(e);//存在callback才传递数据 否则不传
//             }
//         })
// }
// 获取体脂称客户信息
function GetFatModel(data,callback){
    wx.request({
        url: config.minapp_url + '/GetFatModelNew',
        data: {
          tel: data.phone,
          openid: data.openid
        },
        method:'GET',
        success: function(e){
                callback&&callback(e);//存在callback才传递数据 否则不传
            }
        })
}

// 保存体脂称客户体重明细数据
function AddFatDetail(data,callback){
    wx.request({
        url: config.minapp_url + '/AddFatDetailNew',
        data: {
          fatid:data.fatid,
          visceralFatLevel: data.visceralFatLevel,//内脏脂肪等级
          basalMetabolism: data.basalMetabolism,//基础代谢
          protein: data.protein,//蛋白质率
          bodyAge: data.bodyAge,//身体年龄
          bodyScore: data.bodyScore,//身体得分
          bodyStyle: data.bodyStyle,//身体类型
          age: data.age,//年龄
          sex: data.sex,//性别 男-1，女-2
          height: data.height,//身高 
          weight: data.weight,//体重 
          bmi: data.bmi,//体质指数
          pbf: data.pbf,//脂肪率
          water: data.water,//水分率
          bone: data.bone,//骨骼量
          rateOfMuscle: data.rateOfMuscle,//肌肉率 %
          visceralFat:data.visceralFat,//内脏脂肪
          resistance50k:data.resistance50k,//电阻
          bmiName:data.bmiName,
          pbfName:data.pbfName,
          basalMetabolismName:data.basalMetabolismName,
          bodyAgeName:data.bodyAgeName,
          visceralFatName:data.visceralFatName,
          waterName:data.waterName,
          boneName:data.boneName,
          proteinName:data.proteinName,
          rateOfMuscleName:data.rateOfMuscleName
        },
        method:'POST',
        success: function(e){
                callback&&callback(e);//存在callback才传递数据 否则不传
            }
        })
}
// 查看体脂称客户明细列表
function GetFatDetailList(data,callback){
    wx.request({
        url: config.minapp_url + '/GetFatDetailList',
        data: {
          fatid: data.fatid,
        //   fatid: 264,
          startdate: data.startdate,
          enddate: data.enddate,
        },
        method:'GET',
        success: function(e){
                callback&&callback(e);//存在callback才传递数据 否则不传
            }
        })
}
//获取用户最新一次秤重记录
// http://wftpay.ssgddsj.com/MinApp/GetLastFatDetail?fatid=21151
function GetLastFatDetail(fatid,callback){
    wx.request({
        url: config.minapp_url + '/GetLastFatDetail',
        data: {
          fatid: fatid,
        },
        method:'GET',
        success: function(e){
                callback&&callback(e);//存在callback才传递数据 否则不传
            }
        })
}
function GetDistinctFatDetailDate(fatid,callback){
    wx.request({
        url: config.minapp_url + '/GetDistinctFatDetailDate',
        data: {
          fatid: fatid,
        },
        method:'GET',
        success: function(e){
                callback&&callback(e);//存在callback才传递数据 否则不传
            }
        })
}
//查看体脂称客户明细列表(分页)
// lastWeight  上一次的体重值
// http://localhost:49864/MinApp/GetFatDetailListPage?fatid=10950&startdate=2021-07-01&enddate=2021-07-29&pageSize=2&pageIndex=0  分页接口地址

function GetFatDetailListPage(data,callback){
    wx.request({
        url: config.minapp_url + '/GetFatDetailListPage',
        data: {
            fatid:data.fatid,
            // fatid: 264,
            startdate: data.startdate,
            enddate: data.enddate,
            pageSize:data.pageSize,
            pageIndex:data.pageIndex,
        },
        method:'GET',
        success: function(e){
                callback&&callback(e);//存在callback才传递数据 否则不传
            }
    })
}


// 添加体围
function AddAround(data,callback){
    wx.request({
        url: config.minapp_url + '/AddAround',
        data: {
               fatid:data.fatid,
               bust:data.bust,
               waist:data.waist,
               hipline:data.hipline,
               highhipline:data.highhipline,
               thighline:data.thighline,
               crusline:data.crusline
        },
        method:'POST',
        success: function(e){
                callback&&callback(e);//存在callback才传递数据 否则不传
            }
    })
    
}
// 保存体脂称客户身围数据
function GetAroundList(data,callback){
    wx.request({
        url: config.minapp_url + '/GetAroundList',
        data: {
          fatid: data.fatid,
        
          startdate:data.startdate,
          enddate:data.enddate,
        },
        method:'GET',
        success: function(e){
                callback&&callback(e);//存在callback才传递数据 否则不传
            }
        })
}
function getBody18(data,callback){
    // hexMD5
    // appid："appKey"(即appId):"lxb61eccea8d773174",
    // appsecret："appSecret":"a1e29a7deaa834aaab9bb493c91ff79210ddf85b"
    // ?api_appKey=lx4ec9b2c924ea7283&api_sign=BD63FF28C0FC3434E552921D85FA8591&api_timestamp=1596527190000&api_version=1.0
    let api_appKey="lxb61eccea8d773174";
    let appSecret="a1e29a7deaa834aaab9bb493c91ff79210ddf85b";
    let api_timestamp=new Date().getTime();
    let api_version="1.0";
    let AscillArray=[api_appKey,api_timestamp,api_version,appSecret];
    let api_sign=hexMD5(AscillArray.sort().join('')).toUpperCase();

    wx.request({
        url: `https://api.leshiguang.com/api/weight/algorithm/v1.0/getWeightIndexCalculateResult?api_appKey=${api_appKey}&api_sign=${api_sign}&api_timestamp=${api_timestamp}&api_version=${api_version}`,
        data: {
            weight:data.weight,
            age:data.age,
            sex:data.sex,
            height:data.height,
            resistance:data.resistance
        },
        method:'POST',
        success: function(e){
                callback&&callback(e);//存在callback才传递数据 否则不传
            }
        })
}
//得到权重指数计算分析结果
function getBodyIndex(data,callback){
    let api_appKey="lxb61eccea8d773174";
    let appSecret="a1e29a7deaa834aaab9bb493c91ff79210ddf85b";
    let api_timestamp=new Date().getTime();
    let api_version="1.0";
    let AscillArray=[api_appKey,api_timestamp,api_version,appSecret];
    let api_sign=hexMD5(AscillArray.sort().join('')).toUpperCase();
    wx.request({
        url: `https://api.leshiguang.com/api/weight/algorithm/v1.0/getWeightIndexCalculateAndAnalysisResult?api_appKey=${api_appKey}&api_sign=${api_sign}&api_timestamp=${api_timestamp}&api_version=${api_version}`,
        data: {
            weight:data.weight,
            age:data.age,
            sex:data.sex,
            height:data.height,
            weightUnit:1,//1-kg  2-斤  3-磅  4-英石
            resistance:data.resistance
        },
        method:'POST',
        success: function(e){
                callback&&callback(e);//存在callback才传递数据 否则不传
            }
        })
}

//绑定用户设备
function tzcBindDevice(id,callback){
    console.log("绑定id",id);
    let api_appKey="lxb61eccea8d773174";
    let appSecret="a1e29a7deaa834aaab9bb493c91ff79210ddf85b";
    let api_timestamp=new Date().getTime();
    let api_version="1.0";
    let AscillArray=[api_appKey,api_timestamp,api_version,appSecret];
    let api_sign=hexMD5(AscillArray.sort().join('')).toUpperCase();
    console.log(`https://api.leshiguang.com/api/device/bind/v1.0/bindDevice?api_appKey=${api_appKey}&api_sign=${api_sign}&api_timestamp=${api_timestamp}&api_version=${api_version}`);

    wx.request({
        url: `https://api.leshiguang.com/api/device/bind/v1.0/bindDevice?api_appKey=${api_appKey}&api_sign=${api_sign}&api_timestamp=${api_timestamp}&api_version=${api_version}`,
        data: {
            deviceKeyType:"id",
            // deviceKey:id,
            deviceKey:"A4C138A6EDE4",
        },
        method:'POST',
        success: function(e){
                callback&&callback(e);//存在callback才传递数据 否则不传
            }
        })
}


//获取openid
function GetOpenid(code,callback){
    wx.request({
        url:config.minapp_url + '/AuthCode2Session',
        data:{
            js_code: code,
            pay_mch: config.pay_mch
        },
        method:'GET',
        success: function(e){
            callback&&callback(e);//存在callback才传递数据 否则不传
        }
    })
}

//获取用户电话号码
function GetUserPhone(openid,session_key,encryptedData,iv,callback){
    wx.request({
        url: config.minapp_url + '/GetPhoneNumberNew',
        data:{
          openid,
          session_key,
          encryptedData:encryptedData,
          iv:iv,
          pay_mch:config.pay_mch
        },
        method:'POST',
        success: function(result){
                callback&&callback(result);//存在callback才传递数据 否则不传
            }
        })
}

//获取首页数据
function getHomeData(callback){
    wx.request({
        url: config.minapp_url + '/GetFatArtList',
        method:'GET',
        success: function(result){
                callback&&callback(result);//存在callback才传递数据 否则不传
            }
        })
}   

//保存身体评测问卷
function addQuestionnaire(data,callback){
    wx.request({
        url: config.minapp_url + '/AddQuestion',
        data:{
            fatid:data.fatid,
            question1:data.question1,
            question2:data.question2,
            question3:data.question3,
            question4:data.question4,
            question5:data.question5,
            question6:data.question6,
            question7:data.question7,
            height:data.height,
            weight:data.weight,
            question0:data.question0
        },
        method:'POST',
        success: function(result){
                callback&&callback(result);//存在callback才传递数据 否则不传
            }
        })
}


module.exports={
    addQuestionnaire,
    getHomeData,
    GetUserPhone,
    GetOpenid,
    AddFat,
    UpdateFat,
    GetFatModel,
    AddFatDetail,
    GetFatDetailList,
    AddAround,
    GetAroundList,
    getBody18,
    getBodyIndex,
    GetFatDetailListPage,
    GetLastFatDetail,
    GetDistinctFatDetailDate,
    tzcBindDevice
}   